package com.zxm.ex.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxm.ex.common.exception.BackendException;
import com.zxm.ex.common.utils.ToolUtils;
import com.zxm.ex.common.vo.BasePage;
import com.zxm.ex.user.dao.entity.MoocUserT;
import com.zxm.ex.user.dao.mapper.MoocUserTMapper;
import com.zxm.ex.user.vo.MoocUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private MoocUserTMapper moocUserTMapper;
    @Override
    public MoocUserVo getUserInfo(String userId) {
        QueryWrapper wapper=new QueryWrapper<>();
        wapper.eq("UUID", ToolUtils.str2Int(userId));
        MoocUserT moocUserT = moocUserTMapper.selectOne(wapper);
        MoocUserVo vo = new MoocUserVo();
        BeanUtils.copyProperties(moocUserT,vo);
        return vo;
    }

    @Override
    public IPage<MoocUserVo> getUserList(BasePage basePage) throws BackendException {
        Page page = new Page(basePage.getPageNum(),basePage.getSize());
        IPage iPage = moocUserTMapper.selectPage(page, null);
        List<MoocUserT> records = iPage.getRecords();
        List<MoocUserVo> vos  = records.stream().map(moocUserT -> {
            MoocUserVo vo = new MoocUserVo();
            BeanUtils.copyProperties(moocUserT,vo);
            return vo;
        }).collect(Collectors.toList());
        iPage.setRecords(vos);
        return iPage;
    }
}
